package patrol.mobile.com.mobilepatrol.activity.toll;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.office.HtmlWebViewActivity;
import patrol.mobile.com.mobilepatrol.activity.office.NewsListActivity;
import patrol.mobile.com.mobilepatrol.adapter.TollAndServiceAreaAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.TollAndServiceAreaBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by K on 2018/9/13.
 * des: 收费站以及服务区
 */
public class TollAndServiceAreaActivity extends CommonListActivity {

    public static final String TITLE = "TITLE";
    public static final String AREA_ID = "AREA_ID";
    private String title;
    private List<TollAndServiceAreaBean> tollAndServiceAreaBeans;

    public static void show(Context context, String title) {
        Intent intent = new Intent(context, TollAndServiceAreaActivity.class);
        intent.putExtra(TITLE, title);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        super.initView();
        title = getIntent().getStringExtra(TITLE);
        tv_center_title.setText(title);
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new TollAndServiceAreaAdapter(this, baseUrl);
    }

    @Override
    protected String initTitleName() {
        return "";
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(TollAndServiceAreaActivity.this, TollAndServiceDetailActivity.class);
                //收费站信息
                intent.putExtra(TITLE, title);
                intent.putExtra(AREA_ID, tollAndServiceAreaBeans.get(position).getId());
                startActivity(intent);
            }
        });
        if (title.equals("收费站信息")) {
            //收费站信息
            requestData(Urls.GET_ROUTE_TOLL_GATE_LIST);
        } else {
            //服务区
            requestData(Urls.GET_ROUTE_SERVICE_AREA_LIST);
        }
    }

    private void requestData(String url) {
        HttpSender sender = new HttpSender(this, baseUrl, url);
        sender.requestPost("areaName", "", new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                tollAndServiceAreaBeans = GsonUtil.jsonToList(response, TollAndServiceAreaBean.class);
                mAdapter.updateItems(tollAndServiceAreaBeans);
            }
        });
    }
}
